package com.xhui.vimarket.zkplus.zul.attr.rule;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.val.ValExprUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attr.bean.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusil;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp;

/**
 * AttrValueLengthRule.
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-3-23
 * @功能说明：
 * 
 */
public final class AttrValueFromRule {
    
    /**
     * 构造函数.
     * 
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    private AttrValueFromRule() {
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judge(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (inst.getParam1().compareTo(sSetFrom) < 0) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeInt(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (NumericUtil.toInt(inst.getParam1()) < NumericUtil.toInt(sSetFrom)) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeLong(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (NumericUtil.toLong(inst.getParam1()) < NumericUtil.toLong(sSetFrom)) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeDouble(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (NumericUtil.toDouble(inst.getParam1()) < NumericUtil.toDouble(sSetFrom)) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeDate(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (inst.getParam1().compareTo(sSetFrom) < 0) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeDatetime(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (ValExprUtil.isExpr(sSetFrom)) { // 表达式处理
                        sSetFrom = ValExprUtil.getExprVal(sSetFrom);
                    }
                    if (inst.getParam1().compareTo(sSetFrom) < 0) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgeTime(ArrayList<AttrInst> valList, Component rootComp) {
        RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            String sSetFrom = AttrCompUtil.getCompAttrStr(rootComp, AttrBaseComp.COMP_VALUE_FROM);
            if (sSetFrom != null && !StringUtils.startsWith(sSetFrom, "$")) { // 有配置，需判断
                AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    if (inst.getParam1().compareTo(sSetFrom) < 0) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值必须大于等于:" + sSetFrom);
                    }
                }
            }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgePassword(ArrayList<AttrInst> valList, Component rootComp) {
        return judge(valList, rootComp);
    }
}
